module.exports = function(sequelize, DataTypes) {
  var Group = sequelize.define(
    'Group',
    {
      source: {
        type: DataTypes.ENUM('wexin', 'user'),
        defaultValue: 'wexin'
      },
      state: {
        type: DataTypes.ENUM('normal', 'disbanded'),
        defaultValue: 'normal'
      },
      ownerId: {
        type: DataTypes.INTEGER
      },
      openGroupId: {
        type: DataTypes.STRING(100)
      },
      name: {
        type: DataTypes.STRING(50)
      },
      avatarUrl: {
        type: DataTypes.STRING
      }
    },
    {
      tableName: 'groups',
      paranoid: true,
      indexes: [
        {
          name: 'ownerId_idx',
          fields: ['ownerId']
        },
        {
          name: 'openGroupId_idx',
          fields: ['openGroupId']
        }
      ],
      comments: '群组表'
    }
  )
  Group.associate = function(models) {
    this.belongsToMany(models.User, {
      as: 'Users',
      through: 'UserGroup',
      foreignKey: 'groupId'
    })
  }
  return Group
}
